package library.and.xps.com.step.view.fragment;

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.github.lguipeng.library.animcheckbox.AnimCheckBox;
import com.jude.utils.JUtils;
import com.lvfq.pickerview.TimePickerView;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import org.greenrobot.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import library.and.xps.com.step.R;
import library.and.xps.com.step.m.model.CreateActiveParams;
import library.and.xps.com.step.util.PikerUtils;

public class ActiveFormatFragment extends Fragment implements AnimCheckBox.OnCheckedChangeListener, View.OnClickListener {
    AnimCheckBox animCheckBoxStandard;
    AnimCheckBox animCheckBoxTargetRace;
    EditText et_goal;
    AutoLinearLayout al_goal;
    AutoRelativeLayout rl_start_time, rl_end_time;
    CreateActiveParams createActiveParams;
    TextView tv_start_time, tv_end_time;
    String paramStartTimeStr;
    String paramEndTimeStr;
    Date startDate;
    Date endDate;
    ObjectAnimator objectAnimator;
    String hintText = "请输入目标步数";

    TimePickerView.Type activeTypeEnum;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_setup_active_format, null);
        animCheckBoxStandard = (AnimCheckBox) view.findViewById(R.id.cb_standard);
        animCheckBoxTargetRace = (AnimCheckBox) view.findViewById(R.id.cb_target_race);
        tv_start_time = (TextView) view.findViewById(R.id.tv_start_time);
        tv_end_time = (TextView) view.findViewById(R.id.tv_end_time);
        animCheckBoxStandard.setOnCheckedChangeListener(this);
        animCheckBoxTargetRace.setOnCheckedChangeListener(this);
        et_goal = (EditText) view.findViewById(R.id.et_goal);
        et_goal.setHint(hintText);
        al_goal = (AutoLinearLayout) view.findViewById(R.id.al_goal);
        rl_start_time = (AutoRelativeLayout) view.findViewById(R.id.rl_start_time);
        rl_start_time.setOnClickListener(this);
        rl_end_time = (AutoRelativeLayout) view.findViewById(R.id.rl_end_time);
        rl_end_time.setOnClickListener(this);
        createActiveParams = new CreateActiveParams();
        objectAnimator = new ObjectAnimator().ofFloat(al_goal, "alpha", 0.0f, 1.0f);
        initActiveTime();

        return view;
    }

    public void setGoalEditText(String hintText) {
        this.hintText = hintText;
        if (et_goal != null) et_goal.setHint(hintText);
    }

    public void setActiveType(TimePickerView.Type type) {
        activeTypeEnum = type;
    }

    void initActiveTime() {
        startDate = new Date();
        endDate = new Date();

        SimpleDateFormat tv_format = new SimpleDateFormat("MM月dd日 HH:mm");//格式化成显示需要的格式

        Calendar startTimeCalender = Calendar.getInstance();
        startTimeCalender.setTime(startDate);
        startTimeCalender.set(Calendar.HOUR_OF_DAY, 0);//设置从0点0分开始
        startTimeCalender.set(Calendar.MINUTE, 0);
        startDate.setTime(startTimeCalender.getTime().getTime());

        Calendar endTimeCalender = Calendar.getInstance();//设置结束时间到次日的23:59
        endTimeCalender.setTime(startDate);
        endTimeCalender.add(Calendar.DAY_OF_MONTH, 2);
        endDate.setTime(endTimeCalender.getTime().getTime() - 60000);

        tv_start_time.setText(tv_format.format(startDate));
        tv_end_time.setText(tv_format.format(endDate));

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");//格式化成服务器需要的格式
        paramStartTimeStr = simpleDateFormat.format(startDate);

        SimpleDateFormat paramEndTime = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        paramEndTimeStr = paramEndTime.format(endDate);

    }

    @Override
    public void onChange(AnimCheckBox animCheckBox, boolean b) {
        if (animCheckBox.getId() == R.id.cb_standard && b) {
            animCheckBoxTargetRace.setChecked(!b);
        } else if (animCheckBox.getId() == R.id.cb_target_race && b) {
            animCheckBoxStandard.setChecked(!b);
        }
        if (animCheckBoxTargetRace.isChecked()) {
            al_goal.setVisibility(View.VISIBLE);
            objectAnimator.start();
        } else {
            al_goal.setVisibility(View.GONE);
            objectAnimator.reverse();
        }
    }

    public boolean next() {
        if (animCheckBoxStandard.isChecked()) {
            createActiveParams.setFormat("标准赛");
            createActiveParams.setExercise("0");
        } else if (animCheckBoxTargetRace.isChecked()) {
            String goal = et_goal.getText().toString();
            if (!TextUtils.isEmpty(goal)) {
                createActiveParams.setExercise(goal);
                createActiveParams.setFormat("目标赛");
            } else {
                Snackbar.make(al_goal, "请输入目标步数", Snackbar.LENGTH_LONG)
                        .setAction("确定", null).show();
                return false;
            }
        }

        createActiveParams.setEndtime(paramEndTimeStr);
        createActiveParams.setStarttime(paramStartTimeStr);
        EventBus.getDefault().post(createActiveParams);

        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_start_time:
                StartTimerPicker();
                break;
            case R.id.rl_end_time:
                EndTimerPicker();
                break;
        }
    }

    void StartTimerPicker() {
        PikerUtils.alertTimerPicker(getActivity(), activeTypeEnum, "yyyy-MM-dd HH:mm", new PikerUtils.TimerPickerCallBack() {
            @Override
            public void onTimeSelect(Date date) {
                if (date.after(endDate)) {
                    JUtils.Toast("开始时间不能晚于结束时间");
                    return;
                }

                if(date.before(new Date())){
                    JUtils.Toast("选择的时间不能早于今天");
                    return;
                }

                startDate = date;
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                paramStartTimeStr = simpleDateFormat.format(date);
                SimpleDateFormat tv_format = new SimpleDateFormat("MM月dd日 HH:mm");
                tv_start_time.setText(tv_format.format(date));
            }
        });
    }

    void EndTimerPicker() {
        PikerUtils.alertTimerPicker(getActivity(), activeTypeEnum, "yyyy-MM-dd HH:mm", new PikerUtils.TimerPickerCallBack() {
            @Override
            public void onTimeSelect(Date date) {
                if (date.before(startDate)) {
                    JUtils.Toast("结束时间不能早于开始时间");
                    return;
                }

                if(date.before(new Date())){
                    JUtils.Toast("选择的时间不能早于今天");
                    return;
                }

                endDate = date;
                date.setTime(date.getTime() + 86340000);
                SimpleDateFormat paramEndTime = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                paramEndTimeStr = paramEndTime.format(date);
                SimpleDateFormat tv_format = new SimpleDateFormat("MM月dd日 HH:mm");
                tv_end_time.setText(tv_format.format(date));
            }
        });
    }
}
